<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input :placeholder="$t('table.placeholder')" clearable v-model="queryForm.query"></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initGetUsersList">{{ $t('table.search') }}</el-button>
      <el-button type="primary" @click="handleDialogValue()">{{ $t('table.adduser') }}</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column v-for="option in options" :key="option.label" :prop="option.prop" :label="$t(`table.${option.label}`)" :width="option.width">
        <template v-if="option.prop === 'mg_state'" v-slot="{ row }">
          <el-switch v-model="row.mg_state" @change="changeState(row)" />
        </template>
        <template v-if="option.prop === 'create_time'" v-slot="{ row }">
          {{ $filters.filterTimes(row.create_time) }}
        </template>
        <template v-if="option.prop === 'action'" v-slot="{ row }">
          <el-button type="primary" size="small" :icon="Edit" @click="handleDialogValue(row)"></el-button>
          <!-- <el-button type="warning" size="small" :icon="Setting"></el-button> -->
          <el-button type="danger" size="small" :icon="Delete" @click="handleDelete(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:currentPage="queryForm.pagenum"
      v-model:page-size="queryForm.pagesize"
      :page-sizes="[5, 10, 15, 20]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <Dialog v-model="dialogVisible" :dialogTitle="dialogTitle" :dialogTabValue="dialogTabValue" @change="initGetUsersList" v-if="dialogVisible" />
</template>

<script setup>
import { ref } from 'vue'
import { Edit, Search, Setting, Delete } from '@element-plus/icons-vue'
import { getUser, changeUserState, deleteUser } from '@/api/user.js'
import { options } from './options.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useI18n } from 'vue-i18n'
import Dialog from './components/dialog.vue'
import { isNull } from '@/utils/filters.js'

// i18n
const i18n = useI18n()

// dialog
const dialogVisible = ref(false)
const dialogTitle = ref('')
const dialogTabValue = ref('')
const handleDialogValue = (row) => {
  if (isNull(row)) {
    dialogTitle.value = '添加用户'
    dialogTabValue.value = {}
  } else {
    dialogTitle.value = '编辑用户'
    dialogTabValue.value = JSON.parse(JSON.stringify(row))
  }
  dialogVisible.value = true
}

// 表格
const queryForm = ref({
  query: '',
  pagenum: 1,
  pagesize: 5,
})
const tableData = ref([])
const initGetUsersList = async () => {
  const res = await getUser(queryForm.value)
  console.log('initGetUsersList ==> res', res)
  tableData.value = res.users
  total.value = res.total
}
initGetUsersList()

// 分页器
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const total = ref(0)
const handleSizeChange = (pagesize) => {
  console.log(`${pagesize} items per page`)
  queryForm.value.pagenum = 1
  queryForm.value.pagesize = pagesize
  initGetUsersList()
}
const handleCurrentChange = (pagenum) => {
  console.log(`current page: ${pagenum}`)
  queryForm.value.pagenum = pagenum
  initGetUsersList()
}

// 开关
const changeState = async (info) => {
  await changeUserState(info.id, info.mg_state)
  ElMessage({
    message: i18n.t('message.updeteSuccess'),
    type: 'success',
  })
  initGetUsersList()
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(i18n.t('dialog.deleteTitle'), 'Warning', {
    confirmButtonText: i18n.t('message.ok'),
    cancelButtonText: i18n.t('message.cancel'),
    type: 'warning',
  })
    .then(async () => {
      await deleteUser(row.id)
      initGetUsersList()
      ElMessage({
        type: 'success',
        message: 'Delete completed',
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: 'Delete canceled',
      })
    })
}
</script>

<style lang="scss" scoped>
.header {
  padding-bottom: 20px;
  box-sizing: border-box;
}
:deep(.el-button) {
  padding: 0 14px;
}
:deep(.el-pagination) {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>
